﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BL;
using DA;

namespace StoreSystem
{
    public partial class ProductForm : System.Web.UI.Page
    {
        DataManager bl = new DataManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string productId = Request.QueryString.Get("ProductId");
                if (!string.IsNullOrEmpty(productId))
                {

                    Products product = bl.GetProduct(int.Parse(productId));
                    txtProductName.Text = product.Name;
                }
            }
        }
        protected void btn_click(object sender, EventArgs e)
        {
            string productId = Request.QueryString.Get("ProductId");
            if (!string.IsNullOrEmpty(productId))  //Edit Product
            {
                bool isAdded;
                isAdded = bl.EditProduct(int.Parse(productId), txtProductName.Text);
                if (isAdded)
                {
                    lblAdded.Visible = true;
                    Response.Redirect("ProductsList.aspx", true);
                }
            }

            else
            {//new Family
                Products product = new Products();
                product.Name = txtProductName.Text;
                product.Description = txtDescription.Text;
                product.ItemPrice = decimal.Parse(txtItemPrice.Text);
                product.QuantityUnitId = int.Parse(txtQuantityUnitId.Text);
                product.InStock = float.Parse(TextBoxInStock.Text);

                bool isAdded;
                isAdded = bl.AddProduct(product);
                if (isAdded)
                {
                    lblAdded.Visible = true;
                    Response.Redirect("ProductsList.aspx", true);
                }
            }
        }
    }
}